package com.xiaoshuidi.cloud.module.guanwang.service.index.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.common.util.json.JsonUtils;
import com.xiaoshuidi.cloud.module.guanwang.service.index.WebApartmentService;
import com.xiaoshuidi.cloud.module.member.api.index.AppIndexApi;
import com.xiaoshuidi.cloud.module.member.enums.AppIndexPageItemEnum;
import com.xiaoshuidi.cloud.module.rooms.api.home.HomeApi;
import com.xiaoshuidi.cloud.module.rooms.api.home.vo.*;
import com.xiaoshuidi.cloud.module.rooms.api.map.MapApi;
import com.xiaoshuidi.cloud.module.rooms.api.map.vo.GetMapRespApiVO;
import com.xiaoshuidi.cloud.module.rooms.api.map.vo.MapFindSearchReqApiVO;
import com.xiaoshuidi.cloud.module.rooms.api.map.vo.MetroRouteRespApiVO;
import com.xiaoshuidi.cloud.module.rooms.api.map.vo.MetroSiteRespApiVO;
import com.xiaoshuidi.cloud.module.rooms.api.roomlayout.RoomLayoutApi;
import com.xiaoshuidi.cloud.module.rooms.api.roomlayout.vo.GetRoomLayoutByApartmentRespVO;
import com.xiaoshuidi.cloud.module.rooms.api.roomlayout.vo.GetRoomLayoutByApartmentsReqVO;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class WebApartmentServiceImpl implements WebApartmentService {

    @Resource
    private AppIndexApi appIndexApi;
    @Resource
    private RoomLayoutApi roomLayoutApi;
    @Resource
    private HomeApi homeApi;
    @Resource
    private MapApi mapApi;

    @Override
    public CommonResult<Object> recommendApartment(@NonNull Integer id, Long apartmentId) {
        log.info("[WEB官网] 获取营销管理-app配置");
        AppIndexPageItemEnum item = AppIndexPageItemEnum.getEnumByIndex(id);
        return appIndexApi.getIndexConfig(item.getIndex(), null, null, apartmentId);
    }

    /**
     * 根据门店列表, 获取门店下的房型, 随机返回5条记录
     *
     * @return
     */
    @Override
    public CommonResult<List<GetRoomLayoutByApartmentRespVO>> choiceRoomLayout() {
        Object checkedData = recommendApartment(AppIndexPageItemEnum.RESOURCE.getIndex(), null).getCheckedData();
        HashMap<String, List<Map<String, Object>>> checkedData1 = (HashMap<String, List<Map<String, Object>>>) checkedData;
        List<Map<String, Object>> items = checkedData1.get("items");
        List<Long> ids = items.stream().map(p -> Long.parseLong(p.get("aptId").toString())).collect(Collectors.toList());
        log.info("[WEB官网] 获取精选房型,门店列表:{}", ids);
        GetRoomLayoutByApartmentsReqVO reqVO = new GetRoomLayoutByApartmentsReqVO();
        reqVO.setIds(ids);
        reqVO.setCount(5);
        CommonResult<List<GetRoomLayoutByApartmentRespVO>> roomLayoutByApartments = null;
        if(ObjectUtil.isNotEmpty(ids)){
            roomLayoutByApartments = roomLayoutApi.getRoomLayoutByApartments(reqVO);
        }
        log.info("[精选房型] :{}", JsonUtils.toJsonString(roomLayoutByApartments));
        return roomLayoutByApartments;
    }

    /**
     * 门店详情
     *
     * @param apartmentId
     * @return
     */
    @Override
    public CommonResult<ApartmentInfoRespApiVO> getApartmentInfo(Long apartmentId) {
        log.info("[WEB官网] 获取门店详情接口调用 WebApartmentController.getApartmentInfo ,门店id:{}", apartmentId);
        return homeApi.getApartmentInfo(apartmentId);
    }

    /**
     * 散房详情
     *
     * @param roomHouseId
     * @return
     */
    @Override
    public CommonResult<RoomHouseInfoRespApiVO> getRoomHouseInfo(Long roomHouseId) {
        log.info("[WEB官网] 获取散房详情接口调用 WebApartmentController.getRoomHouseInfo ,房源id:{}", roomHouseId);
        return homeApi.getRoomHouseInfo(roomHouseId);
    }

    /**
     * 房源详情-同房源其他房间
     *
     * @param roomHouseId
     * @param tenantId
     * @return
     */
    @Override
    public CommonResult<List<OtherRoomsInTheSameListingRespApiVO>> otherRoomsInTheSameListing(Long roomHouseId, Long tenantId) {
        log.info("[WEB官网] 房源详情-同房源其他房间接口调用 WebApartmentController.otherRoomsInTheSameListing ,房源id:{}, 租户id:{}", roomHouseId, tenantId);
        return homeApi.otherRoomsInTheSameListing(roomHouseId, tenantId);
    }

    /**
     * 房源详情-推荐附近房源
     *
     * @param roomHouseId
     * @param tenantId
     * @return
     */
    @Override
    public CommonResult<List<OtherRoomsInTheSameListingRespApiVO>> recommendListingsNearYou(Long roomHouseId, Long tenantId) {
        log.info("[WEB官网] 房源详情-推荐附近房源接口调用 WebApartmentController.recommendListingsNearYou ,房源id:{}, 租户id:{}", roomHouseId, tenantId);
        return homeApi.recommendListingsNearYou(roomHouseId, tenantId);
    }

    @Override
    public CommonResult<GetLayoutInfoRespApiVO> getLayoutInfo(Long apartmentId, Long layoutId) {
        log.info("[WEB官网] 房型详情接口调用 WebApartmentController.recommendListingsNearYou ,门店id:{}, 房型id:{}", apartmentId, layoutId);
        return homeApi.getLayoutInfo(apartmentId, layoutId);
    }

    @Override
    public CommonResult<List<GetMapRespApiVO>> mapFindRoom(Double maxLongitude, Double minLongitude, Double maxLatitude, Double minLatitude, Double zoom, Long tenantId, Boolean isConcentrated, String district, Double monthRentalMin, Double monthRentalMax, Double longitude, Double latitude, Boolean isWhole, Integer bedRoomNum, String publicFacilities, String roomHouseState) {
        log.info("[WEB官网] 地图找房接口调用 WebApartmentController.mapFindRoom");
        return mapApi.mapFindRoom(maxLongitude, minLongitude, maxLatitude, minLatitude,
                tenantId, isConcentrated, district, monthRentalMin, monthRentalMax,
                longitude, latitude, isWhole, bedRoomNum, publicFacilities, roomHouseState, zoom);
    }

    @Override
    public CommonResult<PageResult<GetMapRespApiVO>> mapFindSearch(String searchBox, Boolean isConcentrated, String roomHouseState, Integer pageNo, Integer pageSize, Long tenantId) {
        log.info("[WEB官网] 地图找房-搜索 接口调用 WebApartmentController.mapFindSearch");
        MapFindSearchReqApiVO reqVO = new MapFindSearchReqApiVO();
        reqVO.setSearchBox(searchBox);
        reqVO.setIsConcentrated(isConcentrated);
        reqVO.setRoomHouseState(roomHouseState);
        reqVO.setPageNo(pageNo);
        reqVO.setPageSize(pageSize);
        reqVO.setTenantId(tenantId);
        return mapApi.mapFindSearch(reqVO);
    }

    @Override
    public CommonResult<List<MetroRouteRespApiVO>> getMetroRoute() {
        log.info("[WEB官网] 获取所有地铁线路 接口调用 WebApartmentController.getMetroRoute");
        return mapApi.getMetroRoute();
    }

    @Override
    public CommonResult<List<MetroSiteRespApiVO>> getMetroSite(Long id) {
        log.info("[WEB官网] 获取地铁线路下的站点 接口调用 WebApartmentController.getMetroSite");
        return mapApi.getMetroSite(id);
    }

    @Override
    public CommonResult<PageResult<ConcentratedFindHouseRespApiVO>> concentratedFindHouse(String searchBox, String district, Double monthRentalMin, Double monthRentalMax, Double longitude, Double latitude, Integer sortRule, Boolean isWhole, Integer bedRoomNum, String publicFacilities, String lon, String lat, Integer pageNum, Integer pageSize) {
        log.info("[WEB官网] 集中式-找房 接口调用 WebApartmentController.concentratedFindHouse");
        return homeApi.concentratedFindHouse(searchBox,district,monthRentalMin,monthRentalMax,longitude,latitude,sortRule,isWhole,bedRoomNum,publicFacilities,lon,lat,pageNum,pageSize);
    }

    @Override
    public CommonResult<PageResult<NoConcentratedFindHouseRespApiVO>> noConcentratedFindHouse(String searchBox, String district, Double monthRentalMin, Double monthRentalMax, Double longitude, Double latitude, Integer sortRule, Boolean isWhole, Integer bedRoomNum, String publicFacilities, Long roomAreaId, Integer pageNum, Integer pageSize) {
        log.info("[WEB官网] 分散式-找房 接口调用 WebApartmentController.noConcentratedFindHouse");
        return homeApi.noConcentratedFindHouse(searchBox,district,monthRentalMin,monthRentalMax,longitude,latitude,sortRule,isWhole,bedRoomNum,publicFacilities,roomAreaId,pageNum,pageSize);
    }
}
